package com.jiayang.hhotel.ui;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.NowLiveSearchAdapter;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.NowLiveSearchModel;
import com.jiayang.hhotel.presenter.NowLiveSearchPresenter;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;
import cn.droidlover.xdroidmvp.net.NetError;

/**
 * Created by Administrator on 2017/5/24 0024.
 * 现住查询页面
 */

public class NowLiveSearchActivity extends XActivity<NowLiveSearchPresenter> implements View.OnClickListener {

    private View layout;
    private ImageView icon;
    private TextView tv;

    private EditText search;//搜索栏输入框
    private String searchText = "";//输入框中输入的内容

    private SwipeRefreshLayout refreshLayout;
    private SwipeMenuRecyclerView recyclerView;

    private NowLiveSearchAdapter adapter;

    private int page = 1;

    @Override
    public void initData(Bundle savedInstanceState) {
        layout = findViewById(R.id.ll_data);
        icon = (ImageView) findViewById(R.id.iv_pic);
        tv = (TextView) findViewById(R.id.tv_text);

        ImageView more= (ImageView) findViewById(R.id.iv_more);
        ImageView back= (ImageView) findViewById(R.id.tv_back);
        refreshLayout= (SwipeRefreshLayout) findViewById(R.id.fresh);
        recyclerView= (SwipeMenuRecyclerView) findViewById(R.id.recyclerView);
        search= (EditText) findViewById(R.id.et_search);
        Button searchBt= (Button) findViewById(R.id.bt_search);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        back.setOnClickListener(this);
        more.setOnClickListener(this);
        searchBt.setOnClickListener(this);

        isLoadMore = true;
        initRequest();

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(!isLoadMore){
                    page = 1;
                    initRequest();
                }else{
                    isFresh(false);
                }
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (!recyclerView.canScrollVertically(1)) {// 手指不能向上滑动了
                    if (!isLoadMore) {
                        page++;
                        initRequest();
                    }
                }
            }
        });
    }

    /**
     * 初始化请求
     */
    public void initRequest(int mpGsttype,int mpPickup,int mpIsvip,int mpIsed,int mpIsSecret,int mpIsmem,int mpIsforeign
    ,int mpIsbirthday,int mpIshis,int mpIsfree,int mpIsupgrade,int mpIsdaily,int mpRenttype,int mpIsover,int mpIswalkin){
        refreshLayout.setVisibility(View.VISIBLE);
        layout.setVisibility(View.GONE);
        isFresh(true);
        getP().asyncLoad(Constant.LAUGUAGE_FLAG_CHINESE,null,null,mpGsttype,mpPickup,mpIsvip,mpIsed,mpIsSecret,mpIsmem
                ,null,searchText,mpIsforeign,mpIsbirthday,mpIshis,mpIsfree,mpIsupgrade,mpIsdaily,mpRenttype,mpIsover,mpIswalkin,null,page);
    }

    /**
     * 初始化请求
     */
    public void initRequest(){
        isLoadMore = true;
        initRequest(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
    }

    private boolean isLoadMore;//判断是否正在加载。用于防止加载更多时重复加载
    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        isFresh(false);
        isLoadMore = false;
        if(page == 1){//只有第一次加载不出数据时才会显示占位图
            refreshLayout.setVisibility(View.GONE);
            layout.setVisibility(View.VISIBLE);
            switch (state){
                case 1002://没有数据
                    icon.setImageResource(R.mipmap.ic_error);
                    tv.setText(getString(R.string.no_data));
                    icon.setOnClickListener(null);
                    break;
                case -1://服务器错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.service_error));
                    icon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            initRequest();
                        }
                    });
                    break;
                default://其他错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.net_error));
                    icon.setOnClickListener(null);
                    break;
            }
        }else{//之前请求到有数据
            ToastUtil.Toast(StringUtil.errMsg(state),this);
        }
    }

    private void isFresh(final boolean is){
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(is);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_now_live_search;
    }

    @Override
    public NowLiveSearchPresenter newP() {
        return new NowLiveSearchPresenter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back://回退
                finish();
                break;
            case R.id.iv_more://更多
                getP().createSearchDialog(this);
                break;
            case R.id.bt_search://搜索按钮
                searchText = search.getText().toString();
                if(!StringUtil.isEmpty(searchText)){
                    initRequest();
                }
                break;
        }
    }

    public void setAdapter(List<NowLiveSearchModel.PreceptionListDtoBean> model){
        isFresh(false);
        if(adapter == null){
            adapter = new NowLiveSearchAdapter(this,model,page);
            recyclerView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
        isLoadMore = false;
    }
}
